package org.kl.bf.service.basic;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.ExcelConfig;
import org.kl.bf.entity.basic.ExcelImport;
import org.kl.bf.repository.basic.ExcelConfigDao;
import org.kl.bf.repository.basic.ExcelImportDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.utils.Collections3;

import com.alibaba.fastjson.JSON;

/**
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:11:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class ExcelImportService {

	private ExcelImportDao excelImportDao;
	private ExcelConfigDao excelConfigDao;

	public ExcelImport getExcelImport(String guid) {
		return excelImportDao.findOne(guid);
	}

	public ExcelImport getExcelImport1(String id) {
		return excelImportDao.find(ExcelImport.class, id);
	}

	public ExcelImport findByImportNo(String importNo) {
		return excelImportDao.findByImportNo(importNo);
	}

	public void saveExcelImport(ExcelImport entity) {
		excelImportDao.save(entity);
	}

	public void saveExcelImport(ExcelImport entity, @RequestParam(value = "inserted") String detailInserted,
			@RequestParam(value = "updated") String detailUpdated, @RequestParam(value = "deleted") String detailDeleted) {
		excelImportDao.save(entity);
		// // “新增”和“修改”时检查项目是否唯一
		List<ExcelConfig> inserts = JSON.parseArray(detailInserted, ExcelConfig.class);
		if (Collections3.isNotEmpty(inserts)) {
			for (ExcelConfig a : inserts) {
				a.setExcelImport(entity);
				excelConfigDao.save(a);
			}
		}
		List<ExcelConfig> updates = JSON.parseArray(detailUpdated, ExcelConfig.class);
		if (updates.size() > 0) {
			for (ExcelConfig b : updates) {
				System.out.println(b.toString());
				excelConfigDao.save(b);
			}
		}
		List<ExcelConfig> deletes = JSON.parseArray(detailDeleted, ExcelConfig.class);
		if (Collections3.isNotEmpty(deletes)) {
			excelConfigDao.deleteInBatch(deletes);
		}
	}

	public void deleteExcelImport(String guid) {
		excelImportDao.delete(guid);
	}

	/**
	 * 删除多条记录
	 * 
	 * @param ids
	 */
	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				excelImportDao.delete(id);
				i++;
			}
		}
		return "删除" + i + "条记录";
	}

	public List<ExcelImport> getAllExcelImport() {
		return excelImportDao.findAll();
	}

	public Page<ExcelImport> getUserExcelImport(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<ExcelImport> spec = buildSpecification(searchParams);

		return excelImportDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<ExcelImport> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<ExcelImport> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), ExcelImport.class);
		return spec;
	}

	public List<ExcelImport> getAll(Map<String, Object> searchParams, String sortType, String orderType) {
		Specification<ExcelImport> spec = KLDynamicSpecifications.buildSpecification(searchParams, ExcelImport.class);
		return excelImportDao.findAll(spec, CRUDUtils.buildSort(sortType, orderType));
	}

	
	@Autowired
	public void setExcelImportDao(ExcelImportDao excelImportDao) {
		this.excelImportDao = excelImportDao;
	}

	@Autowired
	public void setExcelConfigDao(ExcelConfigDao excelConfigDao) {
		this.excelConfigDao = excelConfigDao;
	}


}
